package com.eqdd.yiqidian.ui.function.learn

import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.LearnDemandBean
import com.eqdd.yiqidian.bean.RecommendDemandBean
import com.eqdd.yiqidian.bean.rebuild.*
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonCallBack
import com.eqdd.yiqidian.kotlin.imgPathPreview
import com.eqdd.yiqidian.ui.gongneng.renshi.train.TrainRequestDetailActivity
import com.eqdd.yiqidian.ui.yiqixue.info.LearnArticleInfoActivity
import com.eqdd.yiqidian.ui.yiqixue.info.LearnTeacherInfoActivity
import com.eqdd.yiqidian.ui.yiqixue.info.LectureInfoActivity
import com.eqdd.yiqidian.ui.yiqiyue.YueMainActivity
import com.example.library.AutoFlowLayout
import com.example.library.FlowAdapter
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_learn_main_go.*
import kotlinx.android.synthetic.main.layout_flow_tag.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity
import java.util.*

/**
 * Created by jhj on 18-9-14.
 */
class GoLearnMainActivity : BaseActivity() {

    override fun initBinding() {
        setContentView(R.layout.activity_learn_main_go)
        initTopTitleBar(View.VISIBLE, "易企学")

    }


    override fun initData() {

        //推荐讲师
        OkGo.post<HttpResult<List<LearnLectureListBean>>>(HttpConfig.BASE_URL + HttpConfig.LEARN_TEACHER_RECOMMEND)
                .params("type", 0)
                .execute(object : JsonCallBack<HttpResult<List<LearnLectureListBean>>>() {
                    override fun onSuccess(response: Response<HttpResult<List<LearnLectureListBean>>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            if (result.items != null && result.items.isNotEmpty()) {
                                layout_teacher.visibility = View.VISIBLE
                                recyclerViewManager(recyclerView_teacher)
                                val slimAdapterEx = SlimAdapterEx.create()
                                        .register<LearnLectureListBean>(R.layout.list_item_192, SlimInjector<LearnLectureListBean> { data, injector ->
                                            data?.let {
                                                teacher(injector, it.lectureImage, it.lectureName, it.lectureIntroduce, it.lectureType)
                                            }
                                        })
                                        .attachTo(recyclerView_teacher)
                                        .updateData(result.items) as SlimAdapterEx<LearnLectureListBean>?
                                ItemClickSupport.addTo(recyclerView_teacher)
                                        .setOnItemClickListener { recyclerView, position, v ->
                                            val bean = slimAdapterEx?.getDataItem(position)
                                            startActivity<LearnTeacherInfoActivity>(Config.GUID to bean?.lectureGuid)
                                        }

                            } else {
                                layout_teacher.visibility = View.GONE
                            }

                        }
                    }
                })

        //推荐需求
        OkGo.post<HttpResult<List<RecommendDemandBean>>>(HttpConfig.BASE_URL + HttpConfig.LEARN_DEMAND_RECOMMEND)
                .params("page", 0)
                .execute(object : JsonCallBack<HttpResult<List<RecommendDemandBean>>>() {
                    override fun onSuccess(response: Response<HttpResult<List<RecommendDemandBean>>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            if (result.items != null && result.items.isNotEmpty()) {
                                layout_demand.visibility = View.VISIBLE
                                recyclerViewManager(recyclerView_demand)
                                val slimAdapterEx = SlimAdapterEx.create()
                                        .register<RecommendDemandBean>(R.layout.list_item_150, SlimInjector<RecommendDemandBean> { data, injector ->
                                            injector?.with<TextView>(R.id.tv_content) {
                                                it.text = data?.theme
                                            }
                                        })
                                        .attachTo(recyclerView_demand)
                                        .updateData(result.items) as SlimAdapterEx<RecommendDemandBean>?
                                ItemClickSupport.addTo(recyclerView_demand)
                                        .setOnItemClickListener { recyclerView, position, v ->
                                            val bean = slimAdapterEx?.getDataItem(position)
                                            startActivity<TrainRequestDetailActivity>(Config.ID to bean?.id)
                                        }
                            } else {
                                layout_demand.visibility = View.GONE
                            }
                        }
                    }

                })

        //推荐课程
        OkGo.post<HttpResult<List<LearnCommentCourseListBean>>>(HttpConfig.BASE_URL + HttpConfig.LEARN_COURSE_RECOMMEND)
                .params("page", 0)
                .execute(object : JsonCallBack<HttpResult<List<LearnCommentCourseListBean>>>() {
                    override fun onSuccess(response: Response<HttpResult<List<LearnCommentCourseListBean>>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            if (result.items != null && result.items.isNotEmpty()) {
                                layout_course.visibility = View.VISIBLE
                                recyclerViewManager(recyclerView_course)
                                val slimAdapterEx = SlimAdapterEx.create()
                                        .register<LearnCommentCourseListBean>(R.layout.list_item_150, SlimInjector<LearnCommentCourseListBean> { data, injector ->
                                            injector?.with<TextView>(R.id.tv_content) {
                                                it.text = String.format(Locale.CHINA, "【%s】%s", data?.lectureName, data?.lectCourseTitle)
                                            }
                                        })
                                        .attachTo(recyclerView_course)
                                        .updateData(result.items) as SlimAdapterEx<LearnCommentCourseListBean>?
                                ItemClickSupport.addTo(recyclerView_course)
                                        .setOnItemClickListener { recyclerView, position, v ->
                                            val bean = slimAdapterEx?.getDataItem(position)
                                            startActivity<LectureInfoActivity>(Config.ID to bean?.id)
                                        }
                            } else {
                                layout_course.visibility = View.GONE
                            }
                        }
                    }

                })

        //活跃讲师
        OkGo.post<HttpResult<List<TeacherListBean>>>(HttpConfig.BASE_URL + HttpConfig.LEARN_TEACHER_ACTIVE)
                .params("page", 0)
                .execute(object : JsonCallBack<HttpResult<List<TeacherListBean>>>() {
                    override fun onSuccess(response: Response<HttpResult<List<TeacherListBean>>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            if (result.items != null && result.items.isNotEmpty()) {
                                layout_teacher_new.visibility = View.VISIBLE
                                recyclerViewManager(recyclerView_teacher_new)
                                val slimAdapterEx = SlimAdapterEx.create()
                                        .register<TeacherListBean>(R.layout.list_item_192, SlimInjector<TeacherListBean> { data, injector ->
                                            data?.let {
                                                teacher(injector, it.headimage, it.realname, it.courses, it.researchField)
                                            }
                                        })
                                        .attachTo(recyclerView_teacher_new)
                                        .updateData(result.items) as SlimAdapterEx<TeacherListBean>?
                                ItemClickSupport.addTo(recyclerView_teacher_new)
                                        .setOnItemClickListener { recyclerView, position, v ->
                                            val bean = slimAdapterEx?.getDataItem(position)
                                            startActivity<LearnTeacherInfoActivity>(Config.GUID to bean?.userGuid)
                                        }
                            } else {
                                layout_teacher_new.visibility = View.GONE
                            }

                        }
                    }

                })

        //最新需求
        OkGo.post<HttpResult<List<LearnDemandBean>>>(HttpConfig.BASE_URL + HttpConfig.LEARN_DEMAND)
                .params("page", 0)
                .execute(object : JsonCallBack<HttpResult<List<LearnDemandBean>>>() {
                    override fun onSuccess(response: Response<HttpResult<List<LearnDemandBean>>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            if (result.items != null && result.items.isNotEmpty()) {
                                layout_demand_new.visibility = View.VISIBLE
                                recyclerViewManager(recyclerView_demand_new)
                                val slimAdapterEx = SlimAdapterEx.create()
                                        .register<LearnDemandBean>(R.layout.list_item_150, SlimInjector<LearnDemandBean> { data, injector ->
                                            injector?.with<TextView>(R.id.tv_content) {
                                                it.text = String.format(Locale.CHINA, "【%s】%s", data?.company, data?.thetheme)
                                            }
                                        })
                                        .attachTo(recyclerView_demand_new)
                                        .updateData(result.items) as SlimAdapterEx<LearnDemandBean>?
                                ItemClickSupport.addTo(recyclerView_demand_new)
                                        .setOnItemClickListener { recyclerView, position, v ->
                                            val bean = slimAdapterEx?.getDataItem(position)
                                            startActivity<TrainRequestDetailActivity>(Config.ID to bean?.id)
                                        }
                            } else {
                                layout_demand_new.visibility = View.GONE
                            }

                        }
                    }

                })

        //最新课程
        OkGo.post<HttpResult<List<LearnLectureListBean>>>(HttpConfig.BASE_URL + HttpConfig.LEAEN_COURSE)
                .params("page", 0)
                .execute(object : JsonCallBack<HttpResult<List<LearnLectureListBean>>>() {
                    override fun onSuccess(response: Response<HttpResult<List<LearnLectureListBean>>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            if (result.items != null && result.items.isNotEmpty()) {
                                layout_course_new.visibility = View.VISIBLE
                                recyclerViewManager(recyclerView_course_new)
                                val slimAdapterEx = SlimAdapterEx.create()
                                        .register<LearnLectureListBean>(R.layout.list_item_150, SlimInjector<LearnLectureListBean> { data, injector ->
                                            injector?.with<TextView>(R.id.tv_content) {
                                                it.text = String.format(Locale.CHINA, "【%s】%s", data?.lectureName, data?.courseTheme)
                                            }
                                        })
                                        .attachTo(recyclerView_course_new)
                                        .updateData(result.items) as SlimAdapterEx<LearnLectureListBean>?
                                ItemClickSupport.addTo(recyclerView_course_new)
                                        .setOnItemClickListener { recyclerView, position, v ->
                                            val bean = slimAdapterEx?.getDataItem(position)
                                            startActivity<LectureInfoActivity>(Config.ID to bean?.id)

                                        }
                            } else {
                                layout_course_new.visibility = View.GONE
                            }
                        }
                    }

                })

        //最新文章
        OkGo.post<HttpResultRows<LearnArticleListBean>>(HttpConfig.BASE_URL + HttpConfig.LEARN_ARTICLE)
                .params("page", 0)
                .execute(object : JsonCallBack<HttpResultRows<LearnArticleListBean>>() {
                    override fun onSuccess(response: Response<HttpResultRows<LearnArticleListBean>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            if (result.items?.rows != null && result.items.rows.isNotEmpty()) {
                                layout_article.visibility = View.VISIBLE
                                recyclerViewManager(recyclerView_article)
                                val slimAdapterEx = SlimAdapterEx.create()
                                        .register<LearnArticleListBean>(R.layout.list_item_150, SlimInjector<LearnArticleListBean> { data, injector ->
                                            injector?.with<TextView>(R.id.tv_content) {
                                                it.text = String.format(Locale.CHINA, "【%s】%s", data?.upname, data?.title)
                                            }
                                        })
                                        .attachTo(recyclerView_article)
                                        .updateData(result.items.rows as List<*>?) as SlimAdapterEx<LearnArticleListBean>?
                                ItemClickSupport.addTo(recyclerView_article)
                                        .setOnItemClickListener { recyclerView, position, v ->
                                            val bean = slimAdapterEx?.getDataItem(position)
                                            startActivity<LearnArticleInfoActivity>(Config.ID to bean?.id?.toInt())
                                        }
                            } else {
                                layout_article.visibility = View.GONE
                            }

                        }
                    }

                })


    }

    fun recyclerViewManager(recyclerView: RecyclerView) {
        recyclerView.addItemDecoration(DividerLineItemDecoration(this@GoLearnMainActivity))
        recyclerView.layoutManager = object : LinearLayoutManager(this@GoLearnMainActivity) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.setHasFixedSize(true)
    }

    fun teacher(injector: IViewInjector<*>?, img: String, name: String, course: String?, type: String?) {
        injector?.imageCircle(R.id.iv_avatar, img)
                ?.clicked(R.id.iv_avatar) {
                    imgPathPreview(img)
                }
                ?.text(R.id.tv_name, name)
                ?.with<TextView>(R.id.tv_main_course) {
                    if (!course.isNullOrBlank()) {
                        it.visibility = View.VISIBLE
                        it.text = course
                    } else {
                        it.visibility = View.GONE
                    }
                }
                ?.with<AutoFlowLayout<String>>(R.id.autoFlowLayout) {
                    if (!type.isNullOrBlank()) {
                        it.visibility = View.VISIBLE
                        val split = type?.split(",".toRegex())?.dropLastWhile { it.isEmpty() }?.toTypedArray()
                        it.setAdapter(object : FlowAdapter<String>(split) {
                            override fun getView(p0: Int): View {
                                val view = LayoutInflater.from(this@GoLearnMainActivity).inflate(R.layout.layout_flow_tag, null, false)
                                view.tv_tag.text = split?.get(p0)
                                return view
                            }
                        })
                    } else {
                        it.visibility = View.GONE
                    }
                }

    }

    override fun setView() {
        tv_search_course.onClick {
            startActivity<LearnCourseActivity>()
        }
        tv_search_teacher.onClick {
            startActivity<LearnTeacherActivity>()
        }
        tv_search_demand.onClick {
            startActivity<LearnDemandActivity>()
        }
        tv_search_article.onClick {
            startActivity<YueMainActivity>()
        }
    }
}